package com.micro.commonlibrary.tools;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.micro.commonlibrary.BuildConfig;

public class SystemSettingTools {
    //    拨打电话
    public static void toCallPhone(String phone, Activity activity) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri parse = Uri.parse("tel" + phone);
        intent.setData(parse);
        activity.startActivity(intent);
    }

    //    去设置权限
    public static void toSettingPermissions() {
        String brand = Build.BRAND;//手机厂商
        if (TextUtils.equals(brand.toLowerCase(), "redmi") || TextUtils.equals(brand.toLowerCase(), "xiaomi")) {
            gotoMiuiPermission();//小米
        } else if (TextUtils.equals(brand.toLowerCase(), "meizu")) {
            gotoMeizuPermission();
        } else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) {
            gotoHuaweiPermission();
        } else {
            UiTools.getContext().startActivity(getAppDetailSettingIntent());
        }
    }

    // 去联系人
    public static void toContacts(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        Uri uri = Uri.parse("content://contacts/people");
        intent.setData(uri);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 跳转到miui的权限管理页面
     */
    private static void gotoMiuiPermission() {
        try { // MIUI 8
            Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            localIntent.putExtra("extra_pkgname", UiTools.getContext().getPackageName());
            UiTools.getContext().startActivity(localIntent);
        } catch (Exception e) {
            try { // MIUI 5/6/7
                Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                localIntent.putExtra("extra_pkgname", UiTools.getContext().getPackageName());
                UiTools.getContext().startActivity(localIntent);
            } catch (Exception e1) { // 否则跳转到应用详情
                UiTools.getContext().startActivity(getAppDetailSettingIntent());
            }
        }
    }

    /**
     * 跳转到魅族的权限管理系统
     */
    private static void gotoMeizuPermission() {
        try {
            Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
            UiTools.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            UiTools.getContext().startActivity(getAppDetailSettingIntent());
        }
    }

    /**
     * 华为的权限管理页面
     */
    private static void gotoHuaweiPermission() {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            UiTools.getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            UiTools.getContext().startActivity(getAppDetailSettingIntent());
        }

    }

    //跳转
    private static Intent getAppDetailSettingIntent() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", UiTools.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", UiTools.getPackageName());
        }
        return localIntent;
    }

}
